package com.kitty.game.equip.handler;

import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.model.RoleEquipField;
import com.kitty.game.attribute.config.Attribute;
import com.kitty.game.config.Equip;
import com.kitty.game.enter.FiedValue;
import org.nutz.json.Json;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.*;

import static com.kitty.game.utils.Const.*;
//后写
import com.kitty.game.equip.handler.RefineHandler;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Random;
import static com.kitty.game.utils.Const.MONEY_NAME;
import static com.kitty.game.utils.Const.ROLE_RESOURCE_NOT_ENOUGH;
import static com.kitty.game.utils.NotifyModules.NOTIFY_EQUIP_REFORM_OK;

@Component("EquipRefineSuit")
public class EquipRefineSuitHandler extends RefineHandler {
    /**
     * 炼化之后装备的颜色
     */
    private static final String THIS_COLOR = "绿色";
    /**
     * 会被修改的颜色
     */
    private static final String BEFORE_COLOR = "金色";
    /**
     * 炼化需要的水晶
     */
    private static final String CRYSTAL_NAME = "超级绿水晶";
    /**
     * 炼化成功提示
     */
    private static final String NOT_REQ_COLOR = "#R{0}#n没有炼化金属性，无法炼化黄属性。";
    /**
     * 炼化属性的FIELDID
     */
    private static final short THIS_FIELDID = 3074;
    /**
     * 不能跟什么属性重复
     */
    private static final short DIFF_FIELDID = 1026;
    /**
     * 预览炼化属性的FIELDID
     */
    private static final short PRE_THIS_FIELDID = 6402;
    /**
     * 绿装备固定属性
     */
    private static final short FIXED_FIELDID = 2050;
    /**
     * 绿装备预览固定属性
     */
    private static final short PRE_FIXED_FIELDID = 6658;


    @Override
    public String getOperStr() {
        return "炼化黄属性";
    }

    @Override
    public String getBeforeColor() {
        return BEFORE_COLOR;
    }

    @Override
    public String getThisColor() {
        return THIS_COLOR;
    }

    @Override
    public String getCrystal() {
        return CRYSTAL_NAME;
    }

    @Override
    protected void addSuitFixedField(int fixedField, RoleEquip roleEquip, Equip equip, int polor) {
        short fieldId = 0;
        if (polor == 1) {
            fieldId = 10;
        } else if (polor == 2) {
            fieldId = 7;
        } else if (polor == 3) {
            fieldId = 8;
        } else if (polor == 4) {
            fieldId = 14;
        } else if (polor == 5) {
            fieldId = 3;
        }
        FiedValue fiedValue = roleEquip.getFields().get((short) 1).getField().get((short) 268);
        if (fiedValue != null) {
            fiedValue.setValue(polor);
        }
        RoleEquipField roleEquipField = new RoleEquipField();
        roleEquipField.setField(new LinkedHashMap<>());
        roleEquipField.setType((short) fixedField);// 预览固定绿属性
        fiedValue = new FiedValue();
        fiedValue.setType(fieldId);
        Attribute attribute = attrService.getAttr(fieldId);
        fiedValue.setVT(attribute.getType());
        int min = attrService.getSuitFiledMin(fiedValue.getType(), equip.getReq_level(), equip.getPosition());
        int max = attrService.getSuitFiledMax(fiedValue.getType(), equip.getReq_level(), equip.getPosition());
        int value = new Random().nextInt(max - min) + min + 1;
        fiedValue.setValue(value);
        roleEquipField.getField().put(fiedValue.getType(), fiedValue);
//        logger.error("addSuitFixedField=={}", Json.toJson(roleEquipField));
        roleEquip.getFields().put(roleEquipField.getType(), roleEquipField);
    }

    @Override
    public List<Integer> getSuitFieldId(int pos, int polar) {
        if (pos == WEAPON) {
            if (polar == 1) {
                return Arrays.asList(117, 126, 131, 136, 137, 138, 139, 140, 146, 149, 405, 406);
            } else if (polar == 2) {
                return Arrays.asList(127, 117, 132, 136, 137, 138, 139, 140, 144, 150, 405, 406);
            } else if (polar == 3) {
                return Arrays.asList(128, 133, 117, 136, 137, 138, 139, 140, 148, 151, 405, 406);
            } else if (polar == 4) {
                return Arrays.asList(129, 134, 136, 117, 137, 138, 139, 140, 145, 152, 405, 406);
            } else if (polar == 5) {
                return Arrays.asList(130, 135, 136, 137, 117, 138, 139, 140, 147, 153, 405, 406);
            }
        } else if (pos == HELMET) {
            return Arrays.asList(110, 141, 142, 143);
        } else if (pos == ARMOR) {
            return Arrays.asList(50, 51, 52, 53, 54, 56, 57, 58, 59, 60, 110, 170, 171, 172, 173, 174);
        } else if (pos == BOOT) {
            return Arrays.asList(110, 141, 142, 143, 154);
        }
        return null;
    }

    @Override
    public short getPreField() {
        return PRE_THIS_FIELDID;
    }

    @Override
    public short getField() {
        return THIS_FIELDID;
    }

    @Override
    public short getSuitField() {
        return FIXED_FIELDID;
    }

    @Override
    public short getPreSuitField() {
        return PRE_FIXED_FIELDID;
    }

    @Override
    public Short getDiffField() {
        return DIFF_FIELDID;
    }

    @Override
    public String checkResource(Object... objects) {
        Role role = (Role) objects[0];
        String param = (String) objects[1];
        RoleEquip roleEquip = (RoleEquip) objects[2];

        if (Integer.parseInt(param.split("\\|")[1]) <= 0 || Integer.parseInt(param.split("\\|")[1]) > 5) {
            return ERROR_OPTYPE;
        }
        String noEnough = super.checkResource(role, param, roleEquip);
        if (noEnough != null) {
            return noEnough;
        }

        RoleEquipField roleEquipField = roleEquip.getFields().get(getDiffField());
        if (roleEquipField == null) {
            return MessageFormat.format(NOT_REQ_COLOR, roleEquip.getName(), BEFORE_COLOR);
        }
        return null;
    }

    @Override
    public int needCash(short level, int pos) {
        return level < 70 ? 0 : level * level * 24 + 7500;
    }

    @Override
    public String getUpdateTypeStr() {
        return "suit_refine";
    }
}
